﻿using System;
using System.Linq;
using System.Text;
using JKF.BusinessLayer.BLL;
using JKF.Substructure;
using JKF.HandlerLayer;
//using JKF.BusinessLayer.Customer;

namespace JKF.Web.Customer
{
    public partial class Customer_Add : PageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            MODULE_NAME_EN = "CUSTOMER";
        }
        private static StringBuilder sbBuilder = new StringBuilder();
        public string CurstomerLevelOption
        {
            get
            {
                var bllLevel = new bllDIC_LEVEL();
                var levels = bllLevel.GetModelList("");
                sbBuilder.Clear();
                foreach (var level in levels)
                {
                    sbBuilder.AppendFormat("<option value=\"{0}\">{1}</option>", level.LEVEL_CODE, Server.HtmlEncode(level.NAME));
                }
                return sbBuilder.ToString();
            }
        }

        public string CurstomerGroupOptions
        {
            get
            {
                var groupbll = new bllDIC_GROUP();
                //var result = groupbll.GetListArray(string.Format(" SHOP_CODE='{0}' ", bllSYS_USER.CurrentUser.SHOP_CODE)); yhw20140327
                var result = groupbll.GetListArray(string.Format("", bllSYS_USER.CurrentUser.SHOP_CODE));
                sbBuilder.Clear();
                foreach (var groups in result)
                {
                    sbBuilder.AppendFormat("<option value=\"{0}\">{1}</option>", groups.GROUP_KEY, Server.HtmlEncode(groups.NAME));
                }
                return sbBuilder.ToString();
            }
        }

        public string Province
        {
            get
            {
                var bllArea = new bllDIC_AREA();
                return bllArea.GetProvince();
            }
        }

        public string CustomerInfo
        {
            get
            {
                var customerKey = P("customerKey");
                if (customerKey.IsGuidB())
                {
                    var bllCustomer = new bllCUSTOMER();
                    var bllMem = new bllCUSTOMER_MEMORIALDAY();
                    var bllDisease = new bllCUSTOMER_DISEASE();
                    return new
                    {
                        Customer = bllCustomer.GetModel(customerKey.ToGuid()),
                        MemoryDay = bllMem.GetListArray(string.Format(" CUSTOMER_KEY='{0}'", customerKey)),
                        Disease = (from c in bllDisease.GetListArray(string.Format(" CUSTOMER_KEY='{0}'", customerKey)) select new { Code = c.CODE, Name = c.NAME }).ToList()

                    }.ToJson();
                }
                return "null";
            }
        }

        public string Disease
        {
            get
            {
                sbBuilder.Clear();
                var disMgr = new bllDIC_DISEASE();
                var diseases = disMgr.GetListArray("");
                foreach (var disease in diseases)
                {
                    sbBuilder.AppendFormat("<label><input type=\"checkbox\" nm=\"{1}\" value=\"{0}\"/>{1}</lable>",
                        Server.HtmlEncode(disease.CODE),
                        Server.HtmlEncode(disease.NAME));
                }
                return sbBuilder.ToString();
            }
        }

        protected void BtnUpload_Click(object sender, EventArgs e)
        {
            ImageHandler imgHandler = new ImageHandler();
            string msg;

            string filePath = "";

            bool isSucess = imgHandler.UploadPhotos(FileUpload1.PostedFile, out msg, out filePath);
           

            if (isSucess)
            {
                Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "tip", "alert('上传成功')", true);
                LblFile_path.Text = filePath;
                FILE_PATH.Value = filePath;
            }
            else
            {
                Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "tip_fail",string.Format("alert('{0}')",msg), true);
            }

        }
    }
}